<?php
class Answer_Component_Controller_Question_Index extends Phpfox_Component
{
    public function process()
    {
		Phpfox::getService('answer.question')->getSectionMenu();
        $sView = $this->request()->get('view');
        $this->template()
            ->setBreadcrumb(Phpfox::getPhrase('answer.answers'), $this->url()->makeUrl('answer'),false);
        $this->search()->set(array(
                'type' => 'answer',
                'field' => 'id',
                'search_tool' => array(
                    'table_alias' => 'q',
                    'search' => array(
                        'action' => ($this->url()->makeUrl('answer.question', array('view' => $this->request()->get('view')))),
                        'default_value' => Phpfox::getPhrase('answer.seach_question'),
                        'name' => 'search',
                        'field' => array('q.title', 'q.detail')
                    ),
                    'sort' => array(
                        'latest' => array('q.time_stamp', Phpfox::getPhrase('answer.recent')),
                        'most-viewed' => array('q.view', Phpfox::getPhrase('answer.popular')),
                        'random' => array('rand()', Phpfox::getPhrase('answer.random')),
                    ),
                    'show' => array(100, 150, 200)
                )
            )
        );
        $aBrowseParams = array(
            'module_id' => 'answer',
            'alias' => 'q',
            'field' => 'id',
            'table' => Phpfox::getT('brodev_answer_question'),
            'hide_view' => array('my,popular,random')
        );

        switch ($sView) {
            case '':
                if ((!$this->request()->get('tag')) && ($this->request()->get('req3') == "") && ($this->request()->get('search-id') == "")) {
                    $this->url()->send('answer');
                }
                break;
            case 'my':
                Phpfox::isUser(true);
                $this->search()->setCondition('AND q.user_id = ' . Phpfox::getUserId());
                break;
            case 'featured':
                Phpfox::isUser(true);
                $this->search()->setCondition('AND q.featured = 1');
                break;
            case 'friend':
                Phpfox::isUser(true);
                $aFriends = Phpfox::getService('friend.friend')->getMutualFriends(Phpfox::getUserId());
                $aFriends = $aFriends[1];
                if (count($aFriends) > 0) {
                    $sWhere = 'AND q.user_id in (';
                    foreach ($aFriends as $iKey => $aVal) {
                        if ($iKey != 0) $sWhere .= ",";
                        $sWhere .= $aVal['user_id'];
                    }
                    $sWhere .= ")";
                } else {
                    $sWhere = 'And q.user_id = 0';
                }

                $this->search()->setCondition($sWhere);
                break;
        }

        if ($sTag = $this->request()->get('tag')) {
            $this->search()->setCondition('AND q.tag like \'%' . $sTag . '%\'');
            $this->template()->setBreadcrumb("Tag : " .$sTag, $this->url()->makeUrl('answer.question', array('tag' => $sTag)), true);
        }
        //vdd($this->request()->get('req3'));
        if ($this->request()->get('req3') == 'category' && ($iCategoryId = $this->request()->getInt('req4'))) {
            $aCondition = array('c.id =' . $iCategoryId);
            $aCategories = Phpfox::getService('answer.category')->getListNoPage('c.*', $aCondition);
            $aCategories = $aCategories['data'][0];
        }

        if (isset($aCategories['id'])) {


            $iParentId = Phpfox::getService('answer.category')->getParentId($aCategories['id']);
            if ((int)$aCategories['parent_id'] > 0) {

                $this->template()
                    ->setBreadcrumb($aCategories['parent_name'], $this->url()->makeUrl('answer.question.category.' . $aCategories['parent_id']), false);
            }
            $this->template()
                ->setBreadcrumb($aCategories['name'], $this->url()->makeUrl('answer.question.category.' . $aCategories['id']), true);

            $this->search()->setCondition('AND q.category_id in (' . Phpfox::getService('answer.category')->getAllId($aCategories['id']) . ')');

        }
        $this->search()->browse()->params($aBrowseParams)->execute();
        $aQuestions = $this->search()->browse()->getRows();
        Phpfox::getLib('pager')->set(array('page' => $this->search()->getPage(), 'size' => $this->search()->getDisplay(), 'count' => $this->search()->browse()->getCount()));
        
		$this->template()
            ->setTitle(Phpfox::getPhrase('answer.answer'))

            ->setHeader(array(
            'answer.css' => 'module_answer',
            'comment.css' => 'style_css',
            'feed.js' => 'module_feed'
        ))
            ->assign('aQuestions', $aQuestions);
			
			
			

    }
}